package com.weme.common.view

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.weme.common.R
import com.weme.common.base.BaseDialog
import com.weme.common.util.gone

class CustomDialogFragment : BaseDialog() {

    interface DialogClickListener {
        fun onPositiveButtonClick()
        fun onNegativeButtonClick()
    }

    private lateinit var title: String
    private lateinit var content: String
    private lateinit var positiveButtonText: String
    private lateinit var negativeButtonText: String
    private lateinit var listener: DialogClickListener


    companion object {
        fun newInstance(
            title: String,
            content: String,
            positiveButtonText: String,
            negativeButtonText: String,
            listener: DialogClickListener,
            isDismiss:Boolean  = true
        ): CustomDialogFragment {
            val fragment = CustomDialogFragment()

            fragment.isCancelable = isDismiss
            fragment.title = title
            fragment.content = content
            fragment.positiveButtonText = positiveButtonText
            fragment.negativeButtonText = negativeButtonText
            fragment.listener = listener
            return fragment
        }
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog?.setCancelable(isCancelable)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_custom_dialog, container, false)
        val titleTextView = view.findViewById<TextView>(R.id.titleTextView)
        val contentTextView = view.findViewById<TextView>(R.id.contentTextView)
        val positiveButton = view.findViewById<TextView>(R.id.positiveButton)
        val negativeButton = view.findViewById<TextView>(R.id.negativeButton)

        titleTextView.text = title
        contentTextView.text = content
        positiveButton.text = positiveButtonText
        if(TextUtils.isEmpty(negativeButtonText)){
            negativeButton.gone()
        }
        if(TextUtils.isEmpty(title)){
            titleTextView.gone()
        }

        if(TextUtils.isEmpty(content)){
            contentTextView.gone()
        }
        negativeButton.text = negativeButtonText

        positiveButton.setOnClickListener {
            listener.onPositiveButtonClick()
            dismiss()
        }

        negativeButton.setOnClickListener {
            listener.onNegativeButtonClick()
            dismiss()
        }



        return view
    }


}
